package com.yunhe.mybatis.web.user;

import com.yunhe.mybatis.dto.Pageable;
import com.yunhe.mybatis.service.UserService;
import com.yunhe.mybatis.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/list")
public class UserListServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取分页参数
        String page = req.getParameter("page");
        String size = req.getParameter("size");

        int intPage =  null != page ? Integer.parseInt(page) : 1;
        int intSize = null != size ? Integer.parseInt(size) : 5; //页面m默认的大小

        //获取请求参数
        String name = req.getParameter("name");
        String deparmentName = req.getParameter("deparmentName");
        String sex = req.getParameter("sex");

        Integer sexI = (null != sex && !sex.isEmpty()) ? Integer.valueOf(sex) : null;
        Pageable pageable = userService.findForPage(intPage, intSize,name,deparmentName,sexI);
        req.setAttribute("pageable",pageable);
        req.getRequestDispatcher("/WEB-INF/user/list.jsp").forward(req,resp);
    }
}
